package com.lghs.stutor;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Accountinfo extends Activity {

    Spinner spinner_session;
    RatingBar rb_rating;
    Button btnreview;
    ArrayAdapter<String> spinneradapter;
    ArrayList<String> spinItems;
    ProgressDialog pDialog;
    Intent i;
    Bundle b;
    Vital vital = new Vital();

    static String url, url1, username, type, subject;
    float rating;

    // AsyncTask variables
    HttpGet httpGet;
    HttpResponse execute;
    DefaultHttpClient client;
    InputStream content;
    BufferedReader buffer;

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        i = new Intent();
        i.setClass(getApplicationContext(), Home.class);
        i.putExtra("uname", username);
        startActivity(i);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_info);


    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        declaredcomponents();
        new loadaccountinfo().execute();
        listeners();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void declaredcomponents() {
        b = getIntent().getExtras();
        url = "";
        username = "";
        type = "student";
        subject = "";
        rating = 0;
        username = b.getString("uname");
        btnreview = (Button) findViewById(R.id.accountinfo_button_rejoinoldsession);
        rb_rating = (RatingBar) findViewById(R.id.accountinfo_ratingBar_yourtutorrating);
        rb_rating.setEnabled(false);
        spinner_session = (Spinner) findViewById(R.id.accountinfo_spinner_pastsessions);
        spinItems = new ArrayList<String>();
        spinneradapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_checked, spinItems);
        spinner_session.setAdapter(spinneradapter);
    }

    public void listeners() {
        btnreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i = new Intent();
                i.putExtra("uname", username);
                i.putExtra("sessionid", spinner_session.getSelectedItem().toString().substring(0, spinner_session.getSelectedItem().toString().indexOf("-")).trim());
                //i.putExtra("tutorname",)
                i.setClass(getApplicationContext(), ReviewSession.class);
                startActivity(i);
                finish();
                Toast.makeText(getApplicationContext(), "Reviewing sessionID: " + spinner_session.getSelectedItem().toString().substring(0, spinner_session.getSelectedItem().toString().indexOf("-")).trim(), Toast.LENGTH_LONG).show();
            }
        });

    }

    private class loadaccountinfo extends AsyncTask<String, Void, String> {

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Accountinfo.this,R.style.Theme_MyDialog);
            pDialog.setTitle("You will now wait!");
            pDialog.setIcon(com.lghs.stutor.R.drawable.idk);
            pDialog.setMessage("Loading prior sessions\nPlease Wait..");
            pDialog.setIndeterminate(false); // pDialog.setCancelable(true);
            pDialog.setCancelable(false);
            pDialog.show();
            spinItems.clear();
        }

        protected String doInBackground(String... urls) {
            String response = "";
            url = "http://" + vital.host + "/priorsession.php?u=" + username;

            try {

                client = new DefaultHttpClient();
                httpGet = new HttpGet(url);
                execute = client.execute(httpGet);
                content = execute.getEntity().getContent();

                buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    s = s.trim();
                    if(!s.equals(""))
                        spinItems.add(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            url = "http://" + vital.host + "/getrating.php?u=" + username;

            try {

                client = new DefaultHttpClient();
                httpGet = new HttpGet(url);
                execute = client.execute(httpGet);
                content = execute.getEntity().getContent();

                buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {

                    s = s.trim();
                    if(!s.equals(""))
                        rating = Float.parseFloat(s);
                }
            } catch (IOException z) {
                z.printStackTrace();
            }
            return response;
        }

        protected void onPostExecute(String result) {
            spinneradapter.notifyDataSetChanged();
            rb_rating.setRating(rating);
            pDialog.dismiss();
        }

    }

}
